//
//  SleepMusicVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/3/6.
//

#import "SleepMusicVC.h"
#import "DFPlayer.h"
#import "DFPlayerUIManager.h"
@interface SleepMusicVC ()<DFPlayerDelegate,DFPlayerDataSource>
@property (nonatomic,strong) UIImageView * iconView;
@property (nonatomic,strong) UIButton * closeBtn;
@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UILabel * infoLabel;
@property (nonatomic, strong) NSMutableArray<DFPlayerModel *> *dataArray;

@end

@implementation SleepMusicVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViewLayout];
    [self initDFPlayer];
    [self requestData];
}

-(void)setSubViewLayout{
    
    self.view.backgroundColor = RDRGB(0x6B5B47);
    self.navgationView.iconView.image = [UIImage imageNamed:@"Home_titlte_icon_write"];
    self.bgView.hidden = YES;
    self.navgationView.backgroundColor = RDRGB(0x6B5B47);
    self.navgationView.backBtn.hidden = YES;
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.closeBtn];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:self.infoModel.imageUrl]];
    self.nameLabel.text = self.infoModel.title;
    self.infoLabel.text = self.infoModel.describe;
    
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.infoLabel];

}

-(void)requestData{
    
    for (int i = 0; i < self.listArray.count; i ++) {
        SleepGroupListModel *model = self.listArray[i];
        if (self.infoModel == model) {
            DFPlayerModel *playModel = self.dataArray[i];
            [[DFPlayer sharedPlayer] df_playWithAudioId:playModel.audioId];
        }
    }
}

- (void)initDFPlayer{
    [[DFPlayer sharedPlayer] df_initPlayerWithUserId:nil];
    [DFPlayer sharedPlayer].dataSource  = self;
    [DFPlayer sharedPlayer].delegate    = self;
    [DFPlayer sharedPlayer].playMode    = DFPlayerModeOrderCycle;
    [DFPlayer sharedPlayer].isObserveWWAN = YES;
    [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用
    
    DFPlayerUIManager *mgr = [DFPlayerUIManager sharedManager];

    
    [mgr df_sliderWithFrame:CGRectMake( 80 * FMScaleX, HEIGHT - safaHeight - 159 * FMScaleX ,WIDTH - 160 * FMScaleX, 30 * FMScaleX)
      minimumTrackTintColor:[UIColor blackColor]
      maximumTrackTintColor:[UIColor whiteColor]
                trackHeight:4 * FMScaleX
                 thumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"]
                  superView:self.view];
    
    [mgr df_currentTimeLabelWithFrame:CGRectMake(20 * FMScaleX, HEIGHT - safaHeight - 150 * FMScaleX, 50 * FMScaleX, 12 * FMScaleX)
                            textColor:[UIColor whiteColor]
                        textAlignment:(NSTextAlignmentCenter)
                                 font:Regular(14)
                            superView:self.view];
    
    [mgr df_totalTimeLabelWithFrame:CGRectMake(WIDTH - 70 * FMScaleX, HEIGHT - safaHeight - 150 * FMScaleX, 50 * FMScaleX, 12 * FMScaleX)
                          textColor:[UIColor whiteColor]
                      textAlignment:(NSTextAlignmentCenter)
                               font:Regular(14)
                          superView:self.view];
    
    [mgr df_playPauseBtnWithFrame:CGRectMake((WIDTH - 50 * FMScaleX) / 2, HEIGHT - safaHeight - 100 * FMScaleX, 50 * FMScaleX, 50 * FMScaleX) playImage:[UIImage imageNamed:@"Sleep_play_info_puse"] pauseImage:[UIImage imageNamed:@"Sleep_play_info_play"] superView:self.view block:nil];
    
    @WeakObj(self)
    [mgr df_nextBtnWithFrame:CGRectMake((WIDTH - 50 * FMScaleX) / 2 + 100 * FMScaleX, HEIGHT - safaHeight - 100 * FMScaleX + 16 * FMScaleX, 20 * FMScaleX, 18 * FMScaleX) image:[UIImage imageNamed:@"Sleep_play_info_down"] superView:self.view block:^{
        @StrongObj(self)
        [self getDownSongInfo];
    }];
    [mgr df_lastBtnWithFrame:CGRectMake((WIDTH - 50 * FMScaleX) / 2 - 70 * FMScaleX, HEIGHT - safaHeight - 100 * FMScaleX + 16 * FMScaleX, 20 * FMScaleX, 18 * FMScaleX) image:[UIImage imageNamed:@"Sleep_play_info_up"] superView:self.view block:^{
        @StrongObj(self)
        [self getUpSongInfo];
    }];
}

-(void)getUpSongInfo{
    
    DFPlayerModel * model = [[DFPlayer sharedPlayer] currentAudioModel];
    SleepGroupListModel *infoModel = [[SleepGroupListModel alloc]init];
    
    if (model.audioId == 0) {
        infoModel = self.listArray[self.listArray.count - 1];
    }else{
        infoModel = self.listArray[model.audioId -1];
    }
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:infoModel.imageUrl]];
    self.nameLabel.text = infoModel.title;
    self.infoLabel.text = infoModel.describe;
    
}

-(void)getDownSongInfo{
    
    DFPlayerModel * model = [[DFPlayer sharedPlayer] currentAudioModel];
    SleepGroupListModel *infoModel = [[SleepGroupListModel alloc]init];
    if ((model.audioId + 1) > self.listArray.count - 1) {
        infoModel = self.listArray[0];
    }else{
        infoModel = self.listArray[model.audioId + 1];
    }
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:infoModel.imageUrl]];
    self.nameLabel.text = infoModel.title;
    self.infoLabel.text = infoModel.describe;
    
}


- (NSArray<DFPlayerModel *> *)df_audioDataForPlayer:(DFPlayer *)player{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }else{
        [_dataArray removeAllObjects];
    }
    for (int i = 0; i < self.listArray.count; i++) {
        SleepGroupListModel *infoModel = self.listArray[i];
        DFPlayerModel *model = [[DFPlayerModel alloc] init];
        model.audioId = i;
        model.audioUrl = [NSURL URLWithString:infoModel.musicUrl];
        [_dataArray addObject:model];
    }
    return [_dataArray copy];
}

- (DFPlayerInfoModel *)df_audioInfoForPlayer:(DFPlayer *)player{
    
    SleepGroupListModel *model = self.listArray[player.currentAudioModel.audioId];
    NSURL *imageUrl = [NSURL URLWithString:model.imageUrl];
    NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
        
    DFPlayerInfoModel *infoModel = [[DFPlayerInfoModel alloc] init];
    infoModel.audioName = model.title;
    infoModel.audioSinger = model.title;
    infoModel.audioAlbum = model.title;
    infoModel.audioImage = [UIImage imageWithData:imageData];
    return infoModel;
    
}

- (void)df_player:(DFPlayer *)player didGetStatusCode:(DFPlayerStatusCode)statusCode{
    if (statusCode == DFPlayerStatusNoNetwork) {
        FhudShowStr(@"没有网络连接")
    }else if(statusCode == DFPlayerStatusViaWWAN){
//        [self showAlert:@"继续播放将产生流量费用" okBlock:^{
//            [DFPlayer sharedPlayer].isObserveWWAN = NO;
//            [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
//        }];
    }else if(statusCode == DFPlayerStatusTimeOut){
        FhudShowStr(@"请求超时")
    }else if(statusCode == DFPlayerStatusCacheSucc){
//        [self tableViewReloadData];
        return;
    }else if (statusCode == DFPlayerStatusFailed){
        FhudShowStr(@"音频无法播放")
    }
    else{
        NSLog(@"状态码:%lu",(unsigned long)statusCode);
    }
}

- (void)df_playerDidPlayToEndTime:(DFPlayer *)player{
    
    DFPlayerModel * model = [[DFPlayer sharedPlayer] currentAudioModel];
    DFPlayerModel * playModel  = [[DFPlayerModel alloc]init];
    SleepGroupListModel *infoModel = [[SleepGroupListModel alloc]init];
    if ((model.audioId + 1) == self.listArray.count - 1) {
        infoModel = self.listArray[0];
        playModel = self.dataArray[0];
    }else{
        infoModel = self.listArray[model.audioId + 1];
        playModel = self.dataArray[model.audioId + 1];
    }
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:infoModel.imageUrl]];
    self.nameLabel.text = infoModel.title;
    self.infoLabel.text = infoModel.describe;
    [[DFPlayer sharedPlayer] df_playWithAudioId:playModel.audioId];

}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlPlay:
        {
            break;
        }
        case UIEventSubtypeRemoteControlPause:
        {
            break;
        }
        case UIEventSubtypeRemoteControlNextTrack:
        {
            [self getDownSongInfo];
            break;
        }
        case UIEventSubtypeRemoteControlPreviousTrack:
        {
            [self getUpSongInfo];
            break;
        }
            
        default:
            break;
    }
    
}


-(void)closeBtnAction{
    
    [self dismissViewControllerAnimated:YES completion:^{
        [[DFPlayer sharedPlayer]df_deallocPlayer];
    }];
}

-(UIImageView *)iconView{
    
    if (!_iconView) {
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, NavHeight , WIDTH, HEIGHT - NavHeight - 10 * FMScaleX)];
        _iconView.contentMode = UIViewContentModeScaleAspectFill;
        [_iconView setCornerRadius:20 * FMScaleX rectCorner:UIRectCornerTopLeft | UIRectCornerTopRight];
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        blurEffectView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        blurEffectView.alpha = 0.8;
        [_iconView addSubview:blurEffectView];
    }
    return _iconView;
}

-(UIButton *)closeBtn{
    
    if (!_closeBtn) {
        _closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(20 * FMScaleX,NavHeight + 30 * FMScaleX, 30 * FMScaleX, 30 * FMScaleX)];
        [_closeBtn setImage:[UIImage imageNamed:@"Sleep_play_close"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 * FMScaleX, GETMaxY(self.closeBtn) + 100 * FMScaleX, WIDTH - 40 * FMScaleX, 22 * FMScaleX)];
        _nameLabel.textColor = UIColor.whiteColor;
        _nameLabel.font = Semibold(24 * FMScaleX);
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}
-(UILabel *)infoLabel{
    
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 * FMScaleX, GETMaxY(self.nameLabel) + 20 * FMScaleX, WIDTH - 40 * FMScaleX, 180 * FMScaleX)];
        _infoLabel.font = Regular(12 * FMScaleX);
        _infoLabel.textColor = UIColor.whiteColor;
        _infoLabel.textAlignment = NSTextAlignmentCenter;
        _infoLabel.numberOfLines = 0;
    }
    return _infoLabel;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    [super preferredStatusBarStyle];
    return UIStatusBarStyleDarkContent;
}
@end
